//
//  ITTCBDownloadViewController.m
//  InduceTool
//
//  Created by 魏延龙 on 2020/8/31.
//  Copyright © 2020 魏延龙. All rights reserved.
//

#import "ITTCBDownloadViewController.h"
#import "ITTCBTableViewCell.h"
#import "ITDownloadModel.h"
#import "FLDownloadManager.h"

@interface ITTCBDownloadViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UILabel *progressLabel;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSArray *datas;

@end

@implementation ITTCBDownloadViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];

    [self setupDatas];
    [self setupViews];
}

-(void)setupDatas {
    ITDownloadModel *model = [[ITDownloadModel alloc] init];
    model.progress = 0;
    model.downloadUrl = @"http://220.249.115.46:18080/wav/Lovey_Dovey.mp4";
    
    ITDownloadModel *model1 = [[ITDownloadModel alloc] init];
    model1.progress = 0;
    model1.downloadUrl = @"http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4";
    
    ITDownloadModel *model2 = [[ITDownloadModel alloc] init];
    model2.progress = 0;
    model2.downloadUrl = @"http://vjs.zencdn.net/v/oceans.mp4";
    
    ITDownloadModel *model3 = [[ITDownloadModel alloc] init];
    model3.progress = 0;
    model3.downloadUrl = @"https://media.w3.org/2010/05/sintel/trailer.mp4";
    
    ITDownloadModel *model4 = [[ITDownloadModel alloc] init];
    model4.progress = 0;
    model4.downloadUrl = @"http://220.249.115.46:18080/wav/Lovey_Dovey.mp4";
    
    ITDownloadModel *model5 = [[ITDownloadModel alloc] init];
    model5.progress = 0;
    model5.downloadUrl = @"http://220.249.115.46:18080/wav/Lovey_Dovey.mp4";
    
    ITDownloadModel *model6 = [[ITDownloadModel alloc] init];
    model6.progress = 0;
    model6.downloadUrl = @"http://220.249.115.46:18080/wav/Lovey_Dovey.mp4";
    
    ITDownloadModel *model7 = [[ITDownloadModel alloc] init];
    model7.progress = 0;
    model7.downloadUrl = @"http://220.249.115.46:18080/wav/Lovey_Dovey.mp4";
    
    ITDownloadModel *model8 = [[ITDownloadModel alloc] init];
    model8.progress = 0;
    model8.downloadUrl = @"http://220.249.115.46:18080/wav/Lovey_Dovey.mp4";
    
    ITDownloadModel *model9 = [[ITDownloadModel alloc] init];
    model9.progress = 0;
    model9.downloadUrl = @"http://220.249.115.46:18080/wav/Lovey_Dovey.mp4";
    
    ITDownloadModel *model0 = [[ITDownloadModel alloc] init];
    model0.progress = 0;
    model0.downloadUrl = @"http://220.249.115.46:18080/wav/Lovey_Dovey.mp4";
    
    self.datas = @[model, model1, model2, model3, model4, model5, model6, model7, model8, model9, model0];
}
-(void)setupViews {
    UIButton *startBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [startBtn setTitle:@"开始" forState:UIControlStateNormal];
    startBtn.frame = CGRectMake(50, 200, 100, 44);
    [self.view addSubview:startBtn];
    [startBtn addTarget:self action:@selector(startAction) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *pauseBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [pauseBtn setTitle:@"暂停" forState:UIControlStateNormal];
    pauseBtn.frame = CGRectMake(50, 250, 100, 44);
    [self.view addSubview:pauseBtn];
    [pauseBtn addTarget:self action:@selector(pauseAction) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *cancelBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    cancelBtn.frame = CGRectMake(50, 300, 100, 44);
    [self.view addSubview:cancelBtn];
    [cancelBtn addTarget:self action:@selector(cancelAction) forControlEvents:UIControlEventTouchUpInside];
    
    self.progressLabel = [[UILabel alloc] init];
    self.progressLabel.text = @"0";
    [self.view addSubview:self.progressLabel];
    self.progressLabel.frame = CGRectMake(200, 300, 100, 44);
    self.progressLabel.textColor = [UIColor blueColor];
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[ITTCBTableViewCell class] forCellReuseIdentifier:@"ITTCBTableViewCell"];
    [self.view addSubview:self.tableView];
    
}

-(void)startAction{
    [[FLDownloadManager sharedInstance] startSafeDownloadWithURL:[NSURL URLWithString:@"http://220.249.115.46:18080/wav/Lovey_Dovey.mp4"] customPath:nil firstResponse:^(NSURLResponse * _Nonnull response) {
        
    } progress:^(uint64_t receivedLength, uint64_t totalLength, NSInteger remainingTime, float progress) {
        self.progressLabel.text = [NSString stringWithFormat:@"%.2f",progress];
    } error:^(NSError * _Nonnull error) {
        NSLog(@"下载出错 %@",error);
    } complete:^(BOOL downloadFinished, NSString * _Nonnull pathToFile) {
        NSLog(@"下载完成 %@",pathToFile);
        self.progressLabel.text = @"已完成";
    }];
}

-(void)pauseAction{
    
}

-(void)cancelAction{
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.datas.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ITTCBTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ITTCBTableViewCell" forIndexPath:indexPath];
    [cell setupModel:self.datas[indexPath.row]];
    __weak typeof(self) weakSelf = self;
    cell.downloadBlock = ^{
        ITDownloadModel *model = weakSelf.datas[indexPath.row];
        [[FLDownloadManager sharedInstance] startSafeDownloadWithURL:[NSURL URLWithString:model.downloadUrl] customPath:nil firstResponse:^(NSURLResponse * _Nonnull response) {
            
        } progress:^(uint64_t receivedLength, uint64_t totalLength, NSInteger remainingTime, float progress) {
            model.progress = progress;
            NSLog(@"下载进度 index:%zd progress: %.2f", indexPath.row , progress);
        } error:^(NSError * _Nonnull error) {
            NSLog(@"下载出错 %@",error);
        } complete:^(BOOL downloadFinished, NSString * _Nonnull pathToFile) {
            NSLog(@"下载完成 %@",pathToFile);
            self.progressLabel.text = @"已完成";
        }];
    };
    return cell;
}

@end
